import { objUtil } from "../../components/util/objUtil"

/*
 * @Author: kam
 * @Date: 2019-12-21 15:26:01
 * @LastEditTime: 2020-04-08 10:50:11
 * @LastEditors: HXW
 * @Description: UI属性基类
 * @FilePath: /PCM/src/class/ui/attribute.ts
 */
export interface AttributeType {
  id?: string;
  name?: string;
  field?: string;
  capture?: string;
  value?: any;
  type?: string;
  userData?: any;
  children?: any[];
  parent?: any;
  /** 是否必填  */
  isRequire?: boolean /** 是否禁用修改 */;
  isDisable?: boolean;
  isHidden?: boolean;
  isSelect?: boolean;
}

export class Attribute {
  constructor(obj?: AttributeType) {
    obj && Object.assign(this, obj);
  }
  /** 属性唯一标识，建议用guid */
  id: string = objUtil.guid();
  /** 对应的字段名 */
  field: string;
  /** 属性名称，用来属性查询 */
  name: string;
  /** 属性标题 */
  capture: string;
  /** 属性对应的值 */
  value: any;
  /** 属性对应的组件名称 */
  type: string;
  /** 是否禁用修改 */
  isDisable: boolean = false;
  /** 是否隐藏 */
  isHidden: boolean = false;
  /** 是否选中  */
  isSelect: boolean = false;
  /** 是否必填  */
  isRequire: boolean = false;
  /** 对应的子属性  */
  children: any[] = [];
  /**对应的父属性*/
  parent: any;
  /** 对应事件 */
  eventData: string[];
  /**查找*/
  find(callback: (value: Attribute) => boolean, thisArg?: any): Attribute {
    if (callback.call(thisArg, this)) return this;
    if (this.children && this.children.length > 0) {
      for (let c of this.children) {
        let ret = c.find(callback);
        if (ret) return ret;
      }
    }
    return undefined;
  }
}
